package yogiro.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;

import yogiro.dao.CommentDAO;
import yogiro.dao.ReservationDAO;
import yogiro.to.ProductDateTO;
import yogiro.to.ProductPhotoTO;
import yogiro.to.ProductTO;
import yogiro.to.ProductTimeTO;

public class ReservationService {
	
	private ReservationDAO dao ;
	
	public ReservationService(ReservationDAO dao){
		this.dao = dao;
		
		
	}

	public HashMap dateSubstring(String productDate){
		int year = Integer.parseInt(productDate.substring(0,4));
		int month = Integer.parseInt(productDate.substring(4,6));
		int day =Integer.parseInt(productDate.substring(6,8));
		HashMap map = new HashMap();
		map.put("year", year);
		map.put("month", month);
		map.put("day", day);
		return map;
	
	}
	public int monthEndDate(int startYear,int startMonth,int startDay){
		GregorianCalendar gc = new GregorianCalendar(startYear,startMonth,startDay);
		return gc.getActualMaximum(gc.DATE);
	}
	
	//상품등록
	public void registerProduct(ProductTO pto) throws SQLException{
		//상품date구분 - 년,월,일로 구분
		HashMap startMap =dateSubstring(pto.getProductStartDate());//상품시작 date 
		HashMap endMap = dateSubstring(pto.getProductEndDate());//상품 종료 date 
		//상품시작 월,일
		int startYear = (Integer)startMap.get("year");
		int startMonth = (Integer)startMap.get("month");
		int startDay = (Integer)startMap.get("day");
		
		//상품종료 월,일
		int endYear = (Integer)endMap.get("year");
		int endMonth = (Integer)endMap.get("month");
		int endDay = (Integer)endMap.get("day");
		//상품시작 마지막 날 
		int monthEndDay = monthEndDate(startYear, startMonth, startDay);
		
		
		int productNo = dao.selectProductNo(); //시퀀스 넘버 조회
		pto.setProductNo(productNo);//TO 객체에 no 갑 대입
		dao.insertProduct(pto);//DB에 값 입력
		//상품날짜DB 등록
		ProductDateTO pdto = new ProductDateTO();
		ProductTimeTO ptto = new ProductTimeTO();
		pdto.setProductNo(productNo);//상품 넘버 대입
		String [] times = pto.getProductStartTime();
		//상품 사진 등록
		ProductPhotoTO ppto = new ProductPhotoTO();
		ppto.setProductNo(productNo);
		ArrayList list = pto.getPhotoName();
		for(int i = 0; i<list.size();i++){
			ppto.setProductPhotoName((String)list.get(i));
			ppto.setProductPhotoNo(dao.selectProductPhotoNo());
			dao.insertProductPhoto(ppto);
		}
		
		if(startMonth==endMonth){
			for(int i = startDay ; i<=endDay;i++){
				String day = startYear+"/"+startMonth+"/"+i;
				int productDateNo = dao.selectProductDateNo();//상품 date table 넘버 조회
				pdto.setProductDateNo(productDateNo);
				pdto.setProductDate(day);
				dao.insertProductDate(pdto);
				for(int j =0;j<times.length;j++ ){
					String time = times[j];
					ptto.setProductTimeNo(dao.selectProductDateNo());
					ptto.setProductDateNo(productDateNo);
					ptto.setProductRreservationNumber(0);
					ptto.setProductTime(time);
					dao.insertProductTime(ptto);
				}
			}
		}else{
			for(int i = startDay; i<=monthEndDay;i++){
				String day = startYear+"/"+startMonth+"/"+i;
				int productDateNo = dao.selectProductDateNo();//상품 date table 넘버 조회
				pdto.setProductDateNo(productDateNo);
				pdto.setProductDate(day);
				dao.insertProductDate(pdto);
				for(int j =0;j<times.length;j++ ){
					String time = times[j];
					ptto.setProductTimeNo(dao.selectProductDateNo());
					ptto.setProductDateNo(productDateNo);
					ptto.setProductRreservationNumber(0);
					ptto.setProductTime(time);
					dao.insertProductTime(ptto);
				}
			}
			for(int i = 1;i<=endDay;i++){
				String day = endYear+"/"+endMonth+"/"+i;
				int productDateNo = dao.selectProductDateNo();//상품 date table 넘버 조회
				pdto.setProductDateNo(productDateNo);
				pdto.setProductDate(day);
				dao.insertProductDate(pdto);
				for(int j =0;j<times.length;j++ ){
					String time = times[j];
					ptto.setProductTimeNo(dao.selectProductDateNo());
					ptto.setProductDateNo(productDateNo);
					ptto.setProductRreservationNumber(0);
					ptto.setProductTime(time);
					dao.insertProductTime(ptto);
				}
			}
		}		
	}
	//상품수정
	public void modifyProduct(ProductTO pto) throws SQLException{
		
	}
	//상품목록 출력
	public ArrayList listAllProduct() throws SQLException{
		ArrayList productlist = dao.selectAllProduct();
		for(Object o :productlist){
			int i = 1;
			ProductTO pto = (ProductTO)o;
			int productNo = pto.getProductNo();
			ArrayList date = dao.selectDateByProductNo(productNo);
			pto.setDate(date);
			ArrayList time = dao.selectTimeByProductNo(productNo);
			pto.setTime(time);
			ArrayList photo = dao.selectPhotoByProductNo(productNo);
			pto.setPhoto(photo);
			
		}
		return productlist;										 
	}
	public HashMap productByNo(int no) throws SQLException{
		//no로 product 조회
		ProductTO pto = dao.productByNo(no);
		ArrayList date = dao.selectDateByProductNo(no);
		ProductDateTO pdto= (ProductDateTO)date.get(0);
		ArrayList time = dao.selectTimeByProductNo(pdto.getProductDateNo());
		ArrayList photo = dao.selectPhotoByProductNo(no);
		HashMap map = new HashMap();
		map.put("pto", pto);
		map.put("date", date);
		map.put("time", time);
		map.put("photo", photo);
		return map;
		
	}
}
